package com.fliangyun.nsa.nsamanage.service.impl;

import com.alibaba.fastjson.JSON;
import com.fliangyun.nsa.nsamanage.dao.CalendarInfoMapper;
import com.fliangyun.nsa.nsamanage.entities.CalendarInfo;
import com.fliangyun.nsa.nsamanage.entities.LogInfo;
import com.fliangyun.nsa.nsamanage.entities.User;
import com.fliangyun.nsa.nsamanage.service.CalendarInfoService;
import com.fliangyun.nsa.nsamanage.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author fuliangyun@139.com
 * @date 2023年06月26日
 * @description
 */
@Transactional(readOnly = true)
@Service
public class CalendarInfoServiceImpl implements CalendarInfoService {
    @Autowired
    private CalendarInfoMapper calendarInfoMapper;
    @Autowired
    private LogInfoServiceImpl logInfoService;

    @Transactional
    @Override
    public void save(CalendarInfo calendarInfo, Authentication authentication) {
        User user = (User) authentication.getPrincipal();
        calendarInfo.setUserId(user.getId());
        LogInfo logInfo = new LogInfo();
        if (StringUtil.isBlank(calendarInfo.getId())){
            calendarInfo.setId(StringUtil.uuid());
            calendarInfo.setStatus("0");
            calendarInfoMapper.insert(calendarInfo);
            logInfo.setAction("新增生日");
        }else {
            calendarInfoMapper.update(calendarInfo);
            logInfo.setAction("修改生日");
        }
        logInfo.setDetail(JSON.toJSONString(calendarInfo));
        logInfoService.save(logInfo);
    }

    @Transactional
    @Override
    public void delete(String id, Authentication authentication) {
        LogInfo logInfo = new LogInfo();
        logInfo.setAction("删除生日");
        logInfo.setDetail("id: "+id);
        logInfoService.save(logInfo);
        calendarInfoMapper.delete(id);
    }

    @Override
    public List<CalendarInfo> findList(CalendarInfo calendarInfo, Authentication authentication) {
        User user = (User) authentication.getPrincipal();
        calendarInfo.setUserId(user.getId());
        return calendarInfoMapper.findList(calendarInfo);
    }

    @Override
    public CalendarInfo get(String id) {
        return calendarInfoMapper.get(id);
    }
}
